package com.cpi.cornpp.manualcharges.services;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.manualcharges.converter.ManualChargesConverter;
import com.cpi.cornpp.manualcharges.converter.ManualChargesDetailsConverter;
import com.cpi.cornpp.manualcharges.dao.ManualChargesDAO;
import com.cpi.cornpp.manualcharges.dto.ManualChargesDetailsDTO;
import com.cpi.cornpp.manualcharges.exception.ManualChargesException;
import com.cpi.cornpp.manualcharges.view.ManualChargesDetailsView;
import com.cpi.cornpp.manualcharges.view.ManualChargesView;
import com.cpi.cornpp.purchaseorder.dao.PurchaseOrderVersionDAO;
import com.cpi.cornpp.purchaseorder.view.PurchaseOrderVersionView;
import com.cs.common.utils.NumberUtil;
/**
 * Costos Manuales Service
 * 
 * @author Edgar Joao
 *
 */
public class ManualChargesService {	
	/**
	 * Metodo que llena la lista de Ordenes de Compra seleccionados
	 * @param auxList
	 * @param purchaseOrderList
	 * @return
	 */
	public List purchasesSelectedIdsHelper(List auxList, List purchaseOrderList){
		Iterator iterator = null;
		List list = null;
		PurchaseOrderVersionView view = null;		
		list = new ArrayList(0);
		iterator = purchaseOrderList.iterator();
		while(iterator.hasNext()){
			view = (PurchaseOrderVersionView) iterator.next();
			if(auxList.contains(String.valueOf(view.getPurchaseOrderVersionId()))){
				list.add(view);
			}			
		}		
		return list;
	}
	/**
	 * Obtiene una lista de Costos Manuales por id de Version
	 * 
	 * @param versionId
	 * @return
	 * @throws ManualChargesException
	 */
	public List getManualChargesList(int versionId) throws ManualChargesException{
		ManualChargesDAO dao = null;
		ManualChargesConverter converter = null;
		List manualChargesList = null;		
		try{
			dao = new ManualChargesDAO();
			converter = new ManualChargesConverter();
			manualChargesList = new ArrayList(0);
			manualChargesList = converter.convertDTOsToViews(dao.getManualChargesList(versionId));			
		} catch (ManualChargesException manualChargesException) {
			throw manualChargesException;
		} catch (Exception e){
			ManualChargesException manualChargesException = null;
			e.printStackTrace(System.out);
			manualChargesException = new ManualChargesException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw manualChargesException;
		}
		return manualChargesList;
	}
	/**
	 * Metodo que inserta un Cargo Manual
	 * 
	 * @author Edgar Joao
	 * @param manualChargesView
	 * @param manualChargesList
	 * @throws ManualChargesException
	 */
	public void insertManualCharges(ManualChargesView manualChargesView, List manualChargesList) throws ManualChargesException{
		ManualChargesDAO dao = null;
		ManualChargesConverter converter = null;
		ManualChargesDetailsDTO dto = null;
		ManualChargesDetailsView view = null;
		Iterator iterator = null;		
		int manualChargeId;
		try{
			dao = new ManualChargesDAO();
			converter = new ManualChargesConverter();
			//Se inserta el Cargo Manual
			manualChargeId = dao.insertManualCharges(converter.converterManualChargesViewToDto(manualChargesView));			
			manualChargesList = this.parsePurchaseOrderList(manualChargesList, manualChargeId);
			iterator = manualChargesList.iterator();
			if(!manualChargesList.isEmpty()){
				while(iterator.hasNext()){
					view = (ManualChargesDetailsView) iterator.next();
					dto = converter.convertManualChargesDetailsViewToDTOs(view);
					//Se inserta el detalle del Cargo Manual
					dao.insertManualChargesDetails(dto);
				}
			}			
		} catch (ManualChargesException manualChargesException) {
			throw manualChargesException;
		} catch (Exception e){
			ManualChargesException manualChargesException = null;
			e.printStackTrace(System.out);
			manualChargesException = new ManualChargesException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_INSERT);
			throw manualChargesException;
		}
	}
	/**
	 * Metodo que actualiza un cargo manual y su detalle
	 * @author Edgar Joao
	 * @param manualChargesView
	 * @param manualChargesList
	 * @throws ManualChargesException
	 */
	public void updateManualCharges(ManualChargesView manualChargesView, List manualChargesList) throws ManualChargesException{
		ManualChargesDAO dao = null;
		ManualChargesConverter converter = null;
		ManualChargesDetailsDTO dto = null;
		ManualChargesDetailsView view = null;
		Iterator iterator = null;
		String chargeType = null;
		try{
			dao = new ManualChargesDAO();
			converter = new ManualChargesConverter();
			if(manualChargesView.getChargeType().length() > 1){
				chargeType = converter.getChargeTypeCode(manualChargesView.getChargeType());
				manualChargesView.setChargeType(chargeType);
			}
			//Se actualiza el Cargo Manual
			dao.updateManualCharge(converter.converterManualChargesViewToDto(manualChargesView));			
			manualChargesList = this.parsePurchaseOrderList(manualChargesList, manualChargesView.getManualChargeId());
			dao.deleteManualChargesDetail(manualChargesView.getManualChargeId());
			iterator = manualChargesList.iterator();
			if(!manualChargesList.isEmpty()){
				while(iterator.hasNext()){
					view = (ManualChargesDetailsView) iterator.next();										
					dto = converter.convertManualChargesDetailsViewToDTOs(view);
					//if(!view.isNewManualChargeDetail()){
						//Se inserta el detalle del Cargo Manual
						dao.insertManualChargesDetails(dto);
					//}else{
						//Se actualiza el detalle del Cargo Manual
					//	dao.updateManualChargesDetails(dto);
					//}					
				}
			}			
		} catch (ManualChargesException manualChargesException) {
			throw manualChargesException;
		} catch (Exception e){
			ManualChargesException manualChargesException = null;
			e.printStackTrace(System.out);
			manualChargesException = new ManualChargesException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_INSERT);
			throw manualChargesException;
		}
	}	
	//Se pasa la lista de orden de compra a lista detalle de costos manuales
	private List parsePurchaseOrderList(List list, int manualChargeId){
		Iterator iterator = null;
		PurchaseOrderVersionView purchaseOrderVersionView = null;
		ManualChargesDetailsView chargesDetailsView = null;
		List manualChargesDetailList = null;
		
		manualChargesDetailList = new ArrayList(0);		
		iterator = list.iterator();
		while(iterator.hasNext()){			
			purchaseOrderVersionView = (PurchaseOrderVersionView) iterator.next();
			chargesDetailsView = new ManualChargesDetailsView();
			chargesDetailsView.setManualChargeId(manualChargeId);
			chargesDetailsView.setPurchaseOrderVersionId(purchaseOrderVersionView.getPurchaseOrderVersionId().intValue());
			chargesDetailsView.setAmount(purchaseOrderVersionView.getAmount());
			manualChargesDetailList.add(chargesDetailsView);
		}
		return manualChargesDetailList;
	}
	/**
	 * Metodo que elimina un Cargo Manual
	 * 
	 * @param manualChargeId
	 * @throws ManualChargesException
	 */
	public void deleteManualCharges(int manualChargeId) throws ManualChargesException{
		ManualChargesDAO dao = null;
		try{
			dao = new ManualChargesDAO();
			dao.deleteManualCharges(manualChargeId);
		} catch (ManualChargesException manualChargesException) {
			throw manualChargesException;
		} catch (Exception e){
			ManualChargesException manualChargesException = null;
			e.printStackTrace(System.out);
			manualChargesException = new ManualChargesException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_INSERT);
			throw manualChargesException;
		}
	}
	/**
	 * Metodo que obtiene un Cargo Manual por ID
	 * 
	 * @author Edgar Joao
	 * @param manualChargeId
	 * @return
	 * @throws ManualChargesException
	 */
	public ManualChargesView getManualChargesById(int manualChargeId) throws ManualChargesException{
		ManualChargesDAO dao = null;
		ManualChargesConverter converter = null;
		ManualChargesView view = null;
		try{
			dao = new ManualChargesDAO();
			converter = new ManualChargesConverter();			
			view = converter.converterManualChargesDtoToView(dao.getManualChargesById(manualChargeId));
		} catch (ManualChargesException manualChargesException) {
			throw manualChargesException;
		} catch (Exception e){
			ManualChargesException manualChargesException = null;
			e.printStackTrace(System.out);
			manualChargesException = new ManualChargesException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_INSERT);
			throw manualChargesException;
		}
		return view;
	}
	/**
	 * Metodo que obtiene la lista de ordenes de compra
	 * @author Edgar Joao
	 * @param versionId
	 * @param manualChargeId
	 * @return
	 * @throws ManualChargesException
	 */
	public List getPurchaseOrderList(int versionId, int manualChargeId, String chargeType, String amount) throws ManualChargesException{			
		List purchaseOrderList = null;
		PurchaseOrderVersionDAO dao = null;
		ManualChargesDetailsConverter converter = null;
		try {			
			converter = new ManualChargesDetailsConverter();			
			dao = new PurchaseOrderVersionDAO();						
			purchaseOrderList = converter.convertPurchaseOrderVersion(this.getChargeType(chargeType), dao.getPurchasesOrdersVersionsListById(versionId, manualChargeId), amount);					
		} catch (Exception e){
			ManualChargesException manualChargesException = null;
			e.printStackTrace(System.out);
			manualChargesException = new ManualChargesException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_INSERT);
			throw manualChargesException;
		}		
		return purchaseOrderList;
	}
	/**
	 * Obtiene el tipo de Cargo
	 * @author Edgar Joao
	 * @param chargeType
	 * @return
	 */
	private char getChargeType(String chargeType){
		char result = 0;
		if(StringUtils.equals(chargeType,"Ponderado" )){
			result = 'P';
		}else if(StringUtils.equals(chargeType,"Unitario" )){
			result = 'U';
		}else if(StringUtils.equals(chargeType,"Manual" )){
			result = 'M';
		}
		return result;
	}	
	/**
	 * @author Edgar Joao
	 * @param purchaseOrderList
	 * @return
	 */
	public String getTotal(List purchaseOrderList) throws ManualChargesException{
		Iterator iterator = null;
		double total = 0.0;
		PurchaseOrderVersionView view = null;
		try{
			iterator = purchaseOrderList.iterator();
			while(iterator.hasNext()){
				view = (PurchaseOrderVersionView) iterator.next();
				total += NumberUtil.parseDouble(view.getAmount()) ;
			}		
		} catch (Exception e){
			ManualChargesException manualChargesException = null;
			e.printStackTrace(System.out);
			manualChargesException = new ManualChargesException(e , CornPPException.ACTION_LISTS,CornPPException.ACTION_LISTS);
			throw manualChargesException;
		}
		return NumberUtil.convertPrice(total);
	}
	
}
